package com.example.administrator.rxjavarxandroid_retrofit.http.filter;

import android.text.TextUtils;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
import retrofit2.http.Body;
import retrofit2.http.Headers;
import retrofit2.http.POST;
import rx.Observable;

/**
 * Created by chenboge on 16/9/12.
 */
public class ResponseInterceptor implements Interceptor {

    /*
    此interceptor用于处理每次请求中header中数据问题
    本例用于获取header中Set-Cookie参数值
    cookie未更新，导致注销登录时，再次登录还以原始用户
     */

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        Response response = chain.proceed(request);
        //判断请求header中是否有page参数，且值为Login，可以通过在登录的接口上通过header注解添加page参数，用于判断在登录成功后获取cookie值
//        如@Headers({"Auth-Type:Basic", "Page:Login"})
//        @POST("api/login")
//        Observable<BaseDownBean> userLogin(@Body RequestBody requestBody);

//        if (request.header("Page") != null && TextUtils.equals(request.header("Page"), "Login")) {
//            if (!TextUtils.isEmpty(response.header("Set-Cookie"))) {
        //存储登录成功后的cookie值
//                ShareFeranceUtil.getmInstance().putString(App.getAppContext(), Constants.Set_Cookie, response.header("Set-Cookie"));
//            }
//        }
        return response;
    }
}
